package com.jingtai.shop.service.goods.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.CollectStatusEnum;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.enums.GoodsStatusEnum;
import com.jingtai.common.utils.MyBeanUtils;
import com.jingtai.common.webfacade.error.CommonException;
import com.jingtai.core.entity.member.MemberCollectGoods;
import com.jingtai.core.entity.member.MemberCollectStore;
import com.jingtai.core.entity.shop.MerchantsGoods;
import com.jingtai.core.entity.shop.MerchantsGoodsDetail;
import com.jingtai.core.mapper.shop.MerchantsGoodsDetailMapper;
import com.jingtai.shop.service.goods.MerchantsGoodsDetailService;
import com.jingtai.shop.service.goods.MerchantsGoodsService;
import com.jingtai.shop.service.user.MemberCollectGoodsService;
import com.jingtai.shop.webfacade.vo.goodsdetail.GoodsDetailVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 商户商品详情表 服务实现类
 * </p>
 *
 * @author GR123
 * @since 2020-11-29
 */
@Service
public class MerchantsGoodsDetailServiceImpl extends ServiceImpl<MerchantsGoodsDetailMapper, MerchantsGoodsDetail> implements MerchantsGoodsDetailService {

    @Resource
    private MerchantsGoodsDetailService goodsDetailService;
    @Resource
    private MerchantsGoodsService goodsService;
    @Resource
    private MemberCollectGoodsService memberCollectGoodsService;

    /**
     * 根据商品编码查询商品详情
     */
    @Override
    public GoodsDetailVO queryGoodsDetail(String goodsCode,Long userId) {
        MerchantsGoodsDetail goodsDetail = goodsDetailService.getOne(Wrappers.lambdaQuery(MerchantsGoodsDetail.class)
                .eq(MerchantsGoodsDetail::getGoodsCode, goodsCode)
                .eq(MerchantsGoodsDetail::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));

        MerchantsGoods goods = goodsService.getOne(Wrappers.lambdaQuery(MerchantsGoods.class).eq(MerchantsGoods::getGoodsCode, goodsCode)
                .eq(MerchantsGoods::getGoodsState, GoodsStatusEnum.NORMAL.getStatus())
                .eq(MerchantsGoods::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        GoodsDetailVO goodsVO = MyBeanUtils.copyPropertiesChaining(goods, GoodsDetailVO::new);
        goodsVO.setGoodsId(goods.getId());
        GoodsDetailVO goodsDetailVO = MyBeanUtils.copyPropertiesChaining(goodsDetail, goodsVO);
        //查询当前商品收藏状态
        MemberCollectGoods one = memberCollectGoodsService.getOne(Wrappers.lambdaQuery(MemberCollectGoods.class).eq(MemberCollectGoods::getGoodsCode, goodsCode).eq(MemberCollectGoods::getUserId, userId).eq(MemberCollectGoods::getIsCollect, CollectStatusEnum.COLLECTED.getStatus()).eq(MemberCollectGoods::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        if (ObjectUtil.isEmpty(one)) {
            goodsDetailVO.setCollectState(0);
        }else {
            goodsDetailVO.setCollectState(1);
        }
        return goodsDetailVO;
    }
}
